package cn.xqj.pet.controller;

import cn.xqj.basic.exception.BusinessException;
import cn.xqj.pet.dto.msgDto;
import cn.xqj.pet.service.ISearchMasterMsgService;
import cn.xqj.pet.domain.SearchMasterMsg;
import cn.xqj.pet.query.SearchMasterMsgQuery;
import cn.xqj.basic.util.AjaxResult;
import cn.xqj.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后端接口类；
 */
@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {

    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    /**
     * 接口：添加或修改
     *
     * @param searchMasterMsg 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg) {
        try {
            if (searchMasterMsg.getId() != null) {
                searchMasterMsgService.update(searchMasterMsg);
            } else {
                searchMasterMsgService.add(searchMasterMsg);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            searchMasterMsgService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        try {
            searchMasterMsgService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "批量删除失败");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public SearchMasterMsg findOne(@PathVariable("id") Long id) {
        return searchMasterMsgService.findOne(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<SearchMasterMsg> findAll() {
        return searchMasterMsgService.findAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<SearchMasterMsg> pageQuery(@RequestBody SearchMasterMsgQuery query) {
        return searchMasterMsgService.pageQuery(query);
    }


    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request) {
        try {
            searchMasterMsgService.publish(searchMasterMsg, request);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发布失败，审核不通过！");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发布失败，服务器繁忙！");
        }
    }


    // 待审核或驳回
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        query.setState(0);
        return searchMasterMsgService.pageQuery(query);
    }


    // 寻主池
/*@PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> toSearchMasterPool(@RequestBody SearchMasterMsgQuery query){
        query.setState(1);
        //由于寻主池查询条件和待处理查询条件冲突，但是查询的结构不一样，所以寻主池业务需要重新写
        //查询state=1，shopId is null的寻主消息
        return searchMasterMsgService.queryPagePool(query);
    }*/


    // 已完成
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query) {
        query.setState(2);
        return searchMasterMsgService.pageQuery(query);
    }


    // 用户寻主列表
    @PostMapping("/user")
    public PageList<SearchMasterMsg> userSearchMasterMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.userSearchMasterMsg(query, request);
    }


    // 待处理
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request) {
        return searchMasterMsgService.toHandle(query, request);
    }


    @PostMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId) {
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发布失败，服务器繁忙！");
        }
    }

    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody msgDto msgDto) {
        try {
            searchMasterMsgService.accept(msgDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("发布失败，服务器繁忙！");
        }
    }

}
